layui.use(['form','treeSelect','layer','upload'],function(){
    var form = layui.form
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;
    var upload = layui.upload;
    var treeSelect = layui.treeSelect;

    var request = GetRequest();
    $.get("/equipment/channel/" + request.id,function (data) {
        $("#relChannelId").val(data.relChannelId);
        $("#channelId").val(data.channelId);
        $("#equipmentId").val(data.equipmentId);
        //设置场景id
        $("#sceneId").val(data.sceneId);
        $("#channelNo").val(data.channelNo);
        $("#channelName").val(data.channelName);
        $("#saveTime").val(data.saveTime);
        $("#decimalDigit").val(data.decimalDigit);
        $("#originalDataUnit").val(data.originalDataUnit);
        $("#correctedUnit").val(data.correctedUnit);
        $("#rangeCeiling").val(data.rangeCeiling);
        $("#rangeLowerLimit").val(data.rangeLowerLimit);
        $("#upperLimitOfChangeRate").val(data.upperLimitOfChangeRate);
        $("#calibrationCycle").val(data.calibrationCycle);
        $("#calibrationFormula").val(data.calibrationFormula);
        $("#locationNumber").val(data.locationNumber);
        $("#channelDepth").val(data.channelDepth);
        $("#remarks").val(data.remarks);

        //采集通道应用类型
        $.get("/checkitem/list",function (checkItem) {
            $.each(checkItem.data,function (i,ele) {
                if (ele.id == data.collectChannelApplicateTypeId) {
                    $("#collectChannelApplicateType").append('<option selected="selected" value="'+ ele.id +'">'+ ele.name +'</option>');
                } else {
                    $("#collectChannelApplicateType").append('<option value="'+ ele.id +'">'+ ele.name +'</option>');
                }
            });
            form.render('select');
            form.on('select(checkitem)',function (data) {
                $.get("/checkitem/" + data.value,function (data) {
                    $("#channelName").val(data.name);
                    $("#originalDataUnit").val(data.unit);
                });
            })

        });

        //采集设备处理方式
        $.get("/dictionary/list/9c417fb4851d45ceb7cf57544dddca73",function (channelDataTreatmentMode) {
            $.each(channelDataTreatmentMode.data,function (i,ele) {
                if (ele.value == data.channelDataTreatmentMode) {
                    $("#channelDataTreatmentMode").append('<option selected="selected" value="'+ ele.value +'">'+ ele.name +'</option>');
                } else {
                    $("#channelDataTreatmentMode").append('<option value="'+ ele.value +'">'+ ele.name +'</option>');
                }
            });
            form.render('select');
        });

    });



    //修改场景信息
    form.on('submit(add)', function(data){
        console.log(data);
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        $.ajax({
            url: '/equipment/channel/edit',
            data:data.field,
            type:'POST',
            success: function (data) {
                if(data.code == 200) {
                    top.layer.close(index);
                    top.layer.msg("修改成功！");
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                } else {
                    top.layer.close(index);
                    top.layer.msg("修改失败！");
                }
            },
            error:function () {
                top.layer.close(index);
                top.layer.msg("修改失败！");
            }
        });
        return false;
    });

    //获取路径中的参数
    function GetRequest() {
        var url = location.search;         //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            var strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }


});